package org.dxy.infrastructure.persistent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.dxy.api.dto.vo.WorkOrderOptionVo;
import org.dxy.types.enums.WorkOrderStatusEnum;
import org.springframework.stereotype.Service;
import org.dxy.infrastructure.persistent.po.query.WorkOrderViewPo;
import org.dxy.infrastructure.persistent.mapper.WorkOrderViewMapper;
import org.dxy.infrastructure.persistent.service.WorkOrderViewService;

import java.util.List;

/**
 * 工单查询视图服务实现
 */
@Service
public class WorkOrderViewServiceImpl extends MPJBaseServiceImpl<WorkOrderViewMapper, WorkOrderViewPo> implements WorkOrderViewService {
    @Override
    public List<WorkOrderOptionVo> getAvailableWorkOrders() {
        MPJLambdaWrapper<WorkOrderViewPo> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(WorkOrderViewPo::getId, WorkOrderViewPo::getWorkOrderCode, 
                      WorkOrderViewPo::getProductId, WorkOrderViewPo::getProductName)
               .eq(WorkOrderViewPo::getStatus, WorkOrderStatusEnum.RELEASED.getKey());
        return this.selectJoinList(WorkOrderOptionVo.class, wrapper);
    }

    @Override
    public Long findIdByProductId(Long productId) {
        MPJLambdaWrapper<WorkOrderViewPo> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.select(WorkOrderViewPo::getId)
                .eq(WorkOrderViewPo::getProductId, productId);

        Long id = this.selectJoinOne(Long.class,lambdaQueryWrapper);

        if (id == null) {
            throw new IllegalArgumentException("productId不存在：" + productId);
        }
        return id;
    }
}